package com.yami.shop.rights.common.strategy.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yami.shop.bean.enums.RightsType;
import com.yami.shop.bean.event.AddRightsEvent;
import com.yami.shop.bean.event.TransferRightsEvent;
import com.yami.shop.rights.common.dto.PersonRightsDto;
import com.yami.shop.rights.common.model.*;
import com.yami.shop.rights.common.service.PersonRightsExternalService;
import com.yami.shop.rights.common.service.ProdRightsExternalDataService;
import com.yami.shop.rights.common.service.RightsExternalService;
import com.yami.shop.rights.common.strategy.PersonRightsStrategy;
import com.yami.shop.security.common.util.AuthUserContext;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Log4j2
@Service
public class ExternalPersonRightsStrategyImpl implements PersonRightsStrategy {

    @Autowired
    private RightsExternalService rightsExternalService;
    @Autowired
    private PersonRightsExternalService personRightsExternalService;
    @Autowired
    private ProdRightsExternalDataService prodRightsExternalService;

    @Override
    public String rightsType() {
        return RightsType.ELECTRONIC_VOUCHER.value();
    }

    @Override
    public void savePersonRightsData(ProdRights dto) {
        LambdaUpdateWrapper<RightsExternal> rightsExternalLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        rightsExternalLambdaUpdateWrapper.eq(RightsExternal::getId, dto.getProdId());
        rightsExternalLambdaUpdateWrapper.set(RightsExternal::getStatus, "1");
        rightsExternalService.update(rightsExternalLambdaUpdateWrapper);
    }

    @Override
    public Long getSurplusTimes(long id) {
        QueryWrapper<PersonRightsExternal> personRightsExternalQueryWrapper = new QueryWrapper<>();
        personRightsExternalQueryWrapper.eq("person_rights_id",id);
        List<PersonRightsExternal> personRightsExternals = personRightsExternalService.list(personRightsExternalQueryWrapper);
        return ObjectUtil.isNotEmpty(personRightsExternals) ? personRightsExternals.stream().mapToLong(PersonRightsExternal::getTimes).sum() : 0L;
    }

    @Override
    public void addPersonRights(ProdRights prodRights, AddRightsEvent event, long personRightsId) {
        // 增加电子券权益
        log.info("增加电子券权益" + event.getUserId());
        // 获取电子券
        List<ProdRightsExternalData> prodRightsExternalDatas = prodRightsExternalService.getDataByCount(prodRights.getProdId(), prodRights.getDrawCount());
        // 新增券码持有详情
        List<PersonRightsExternal> personRightsExternals = new ArrayList<>();
        prodRightsExternalDatas.forEach(s -> {
            PersonRightsExternal personRightsExternal = new PersonRightsExternal();
            personRightsExternal.setExternalDataId(s.getId());
            personRightsExternal.setEffectiveTime(prodRights.getExchangeEndTime());
            personRightsExternal.setPersonRightsId(personRightsId);
            personRightsExternal.setTimes(prodRights.getDrawCount());
            personRightsExternals.add(personRightsExternal);
        });
        personRightsExternalService.saveBatch(personRightsExternals);

    }

    @Override
    public List<PersonRightsDto> verificationRights(JSONObject qrCodeData) {
        // 修改次数
        ArrayList<PersonRightsDto> personRightsDtos = new ArrayList<>();
        LambdaUpdateWrapper<PersonRightsExternal> personRightsExternalLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        personRightsExternalLambdaUpdateWrapper.eq(PersonRightsExternal::getPersonRightsId,qrCodeData.getString("personRightsId"));
        personRightsExternalLambdaUpdateWrapper.eq(PersonRightsExternal::getExternalDataId,qrCodeData.getString("rightsExternalDataId"));
        personRightsExternalLambdaUpdateWrapper.set(PersonRightsExternal::getStatus,1);
        personRightsExternalLambdaUpdateWrapper.setSql("times = times - " + qrCodeData.getLongValue("count"));
        personRightsExternalService.update(personRightsExternalLambdaUpdateWrapper);
        // 修改电子券使用状态 改为 已核销
        LambdaUpdateWrapper<ProdRightsExternalData> prodRightsExternalDataLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        prodRightsExternalDataLambdaUpdateWrapper.eq(ProdRightsExternalData::getId,qrCodeData.getString("rightsExternalDataId"));
        prodRightsExternalDataLambdaUpdateWrapper.set(ProdRightsExternalData::getStatus,2);
        prodRightsExternalService.update(prodRightsExternalDataLambdaUpdateWrapper);
        PersonRightsDto personRightsDto = new PersonRightsDto();
        personRightsDto.setId(qrCodeData.getLongValue("personRightsId"));
        personRightsDto.setRightsId(qrCodeData.getLongValue("rightsId"));
        personRightsDto.setRemark(qrCodeData.getString("tokenId"));
        // 被核销人
        personRightsDto.setUserId(qrCodeData.getString("userId"));
        // 核销人
        personRightsDto.setHolderUserId(AuthUserContext.get().getUserId());
        personRightsDto.setTimes(qrCodeData.getLongValue("count"));
        personRightsDto.setStatus("2");
        personRightsDto.setStatusUpdate(1);
        personRightsDtos.add(personRightsDto);
        return personRightsDtos;
    }

    @Override
    public void updatePersonRights(Long rights,String personRightsId, String userId, String toUserId) {
        // 电子券 token转移 无 修改逻辑
    }

    @Override
    public void cancelOrder(Long orderNumber) {
        // 电子券 无 回滚次数逻辑

    }

    @Override
    public void updateRightsEffective(List<Long> rightsId) {
        // 修改过期状态
        LambdaUpdateWrapper<PersonRightsExternal> personRightsExternalLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        personRightsExternalLambdaUpdateWrapper.in(PersonRightsExternal::getPersonRightsId,rightsId);
        personRightsExternalLambdaUpdateWrapper.eq(PersonRightsExternal::getStatus,0);
        personRightsExternalLambdaUpdateWrapper.set(PersonRightsExternal::getStatus,3);
        personRightsExternalService.update(personRightsExternalLambdaUpdateWrapper);
    }

    @Override
    public void rightsTransfer(TransferRightsEvent event) {

    }
}
